/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.configuration.netflix.archaius;

import com.netflix.config.*;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;

/**
 *  参考netflix archaius 的官方 wiki 来写一个"拉"方式的Demo
 *  https://hub.fastgit.org/Netflix/archaius/wiki/Features
 *
 *  依赖于三个API实现。
 *  1. 基于“拉”模式的配置源 - 需要实现 {@link PolledConfigurationSource}   接口
 *  2. 基于“拉“模式的配置更新调度器 - 需要实现 {@link AbstractPollingScheduler} 抽象类
 *  3. 安装动态配置源 - {@link DynamicConfiguration} ->{@link ConfigurationManager}
 *
 */
public class PolledConfigurationDemo {

    private static final Long DEFAULT_VALUE = 9L;

    public static void main(String[] args) throws InterruptedException {
        // 基于“拉”模式的配置源
        PolledConfigurationSource source = createMyOwnSource();
        AbstractPollingScheduler scheduler = createMyOwnScheduler();
        ConfigurationManager.install(new DynamicConfiguration(source, scheduler));

        DynamicLongProperty prop = DynamicPropertyFactory.getInstance()
                .getLongProperty("currentTime", DEFAULT_VALUE);

        AtomicLong oldValueRef = new AtomicLong(prop.getValue());

        System.out.println("oldValue : " + oldValueRef.get());

        prop.addCallback(() -> {
            Long oldValue = oldValueRef.get();
            Long newValue = prop.getValue();
            // 保存当前值作为上一次配置内容
            oldValueRef.set(newValue);
            System.out.printf("[线程：%s] oldValue = %d, newValue = %d \n",
                    Thread.currentThread().getName(),
                    oldValue,
                    newValue
            );
        });


        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static AbstractPollingScheduler createMyOwnScheduler() {
        return new ThreadPoolExecutorPollingScheduler();
    }

    private static PolledConfigurationSource createMyOwnSource() {
        return new InMemoryPolledConfigurationSource();
    }
}
